package com.ruoyi.module.system.controller;


import com.ruoyi.core.entity.*;
import com.ruoyi.web.annotations.ApiController;
import com.ruoyi.web.annotations.ApiLog;
import com.ruoyi.web.domain.SysMod;
import com.ruoyi.web.entity.db.*;
import com.ruoyi.web.entity.system.SysModInfoVo;
import com.ruoyi.web.service.SysModColumnService;
import com.ruoyi.web.service.SysModDbService;
import com.ruoyi.web.service.SysModService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@ApiController
@ApiLog
@RestController
@RequestMapping("/sysMod")
public class SysModController {


    @Autowired
    private SysModService sysModService;

    @Autowired
    private SysModDbService sysModDbService;

    @Autowired
    private SysModColumnService sysModColumnService;

    @PostMapping("/getById")
    public Output<SysMod> getById(@Valid @RequestBody SysMod entity) {

        return Output.success(sysModService.getById(entity));

    }

    private List<Creator.CreatorItem> getWhere() {
        List<Creator.CreatorItem> where = new ArrayList<>();
        Creator.CreatorItem item1 = new Creator.CreatorItem(ConnType.and, "modName", OperatorType.like, null);
        Creator.CreatorItem item2 = new Creator.CreatorItem(ConnType.and, "tableName", OperatorType.like, null);
        where.add(item1);
        where.add(item2);
        return where;
    }

    private List<SumInfo> getSums() {
        List<SumInfo> sums = new ArrayList<>();
        sums.add(new SumInfo("modName", SumType.count));
        return sums;
    }

    @PostMapping("/getList")
    public Output<List<SysMod>> getList(@RequestBody BaseMap entity) {

        if (entity == null) {
            entity = new BaseMap();
        }

        QueryCreator queryCreator = new QueryCreator(entity);
        queryCreator.setWhere(getWhere());   //动态构建查询条件（后端构建比较安全）

        List<SysMod> list = sysModService.list(queryCreator);

        return Output.success(list);

    }

    @PostMapping("/getPage")
    public Output<PageInfo<SysMod>> getPage(@RequestBody BaseMap entity) {

        if (entity == null) {
            entity = new BaseMap();
        }

        QueryPageCreator queryPageCreator = new QueryPageCreator(entity);
        queryPageCreator.setWhere(getWhere());   //动态构建查询条件（后端构建比较安全）
        queryPageCreator.setSums(getSums());

        PageInfo<SysMod> page = sysModService.page(queryPageCreator);

        return Output.success(page);

    }


    @PostMapping("/getInfoById")
    public Output<SysModInfoVo> getInfoById(@Valid @RequestBody SysMod entity) {

        return Output.success(sysModService.getInfoById(entity.getModId()));

    }

}
